<?php

namespace App\Repository;
use App\Exceptions\BusinessException;
use App\Models\Promotion\CouponAssignLog;

/**
 * 优惠券数据仓储类
 *
 * @package App\Http\Repository
 * @author WangBin
 * @mail: <849850352@qq.com>
 */
class CouponAssignLogRepository extends BaseRepository
{
    /**
     * 获取发放记录
     *
     * @param int $logId
     * @return int|mixed
     * @throws BusinessException
     */
    public function getById(int $logId)
    {
        return CouponAssignLog::query()->where('log_id', $logId)->first();
    }


    /**
     * 保存发放优惠券记录
     *
     * @param array $data
     * @return int
     */
    public function create(array $data)
    {
        $query = new CouponAssignLog();
        foreach ($data as $field => $value) {
            $query->{$field} = $value;
        }
        return $query->save();
    }

    /**
     * 更新发放优惠券记录
     *
     * @param array $data
     * @return int
     */
    public function update(array $data, int $id)
    {
        $query = CouponAssignLog::query()->find($id);
        foreach ($data as $field => $value) {
            $query->{$field} = $value;
        }
        return $query->save();
    }
}
